#pragma once
#include <vector>
#include <functional>
#include <mutex> 
#include <atomic>

template <class T> 
class EventQueue
{ 
public:
    typedef  std::function<void(T )> Processor; 
    void push(T item)
    {
	m_push_num++; 
	{
	    std::lock_guard<std::mutex> guard(m_mutex);
	    m_data.push_back(item); 
	}
    }

    // single thread 
    void process(Processor processor)
    {
	if (m_data.empty())
	{
	    return ; 
	}
		
        //static std::vector<T> m_cache; 
        {
            std::lock_guard<std::mutex> guard(m_mutex);
            m_data.swap(m_cache);
        }


        for(auto item : m_cache)
        {
            processor(item); 
	    m_process_num++; 
        } 
        m_cache.clear(); 
    }

    void dump()
    {
	printf("push %llu   process %llu \n", m_push_num.load(),m_process_num.load()); 
    }
  
private: 
    std::atomic<uint64_t> m_push_num; 
    std::atomic<uint64_t> m_process_num; 

    std::mutex  m_mutex; 
    std::vector<T> m_data;  
    std::vector<T> m_cache;  
}; 


